import { MainActions } from "../actions";
import { ISelectSymbolRequest, ISelectSymbolResponse } from "../models/main";

export interface State {
  selectSymbolResponse: ISelectSymbolResponse;
  pending: boolean;
}

export const initialState: State = {
  selectSymbolResponse: {
    code: -1,
    data: null,
    msg: ""
  },
  pending: true
};

export function reducer(
  state = initialState,
  action: MainActions.MainActionsUnion
): State {
  switch (action.type) {
    case MainActions.MainActionTypes.SelectSymbol: {
      return {
        ...state,
        pending: true
      };
    }
    case MainActions.MainActionTypes.SelectSymbolSuccess: {
      return {
        ...state,
        selectSymbolResponse: action.payload.res,
        pending: false
      };
    }

    case MainActions.MainActionTypes.SelectSymbolError: {
      return {
        ...state,
        pending: false
      };
    }

    default: {
      return state;
    }
  }
}
